/**
 * 
 */
package com.inspectortime.domain.mock;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.inspectortime.domain.Employee;
import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.Inspector;
import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.ScheduleRule;
import com.inspectortime.domain.impl.InspectorCompanyImpl;
import com.inspectortime.domain.impl.ScheduleRuleImpl;
import com.inspectortime.domain.impl.UserImpl;
import com.inspectortime.domain.type.Name;

@Entity
@Table(name = "INSPECTOR")
@PrimaryKeyJoinColumn(name="USER_ID")
public class MockInspectorImpl extends UserImpl implements Inspector {

	@OneToMany(targetEntity = ScheduleRuleImpl.class, cascade = { CascadeType.ALL })
	private List<ScheduleRule> scheduleRules;
	
	// TODO: Try to move company and phone to EmployeeImpl
	private InspectorCompany company;
	private String phone;	
	private Employee.Status status = Employee.Status.ACTIVE;
	private int limitDrivingPerDayDistance = 20;
	private boolean limitDrivingPerDay;
	
	public MockInspectorImpl() {
	}

	public MockInspectorImpl(Name name) {
		super(name);
	}
	
	@Column( length = 20 )
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column( length = 20 ) // this defaults to: @Enumerated(EnumType.ORDINAL)
	public Status getStatus() {
		return this.status;
	}

	public void setStatus(Status status) {
		this.status = status;
		
	}
	
	@ManyToOne(targetEntity = InspectorCompanyImpl.class, cascade = { CascadeType.ALL })
	public InspectorCompany getCompany() {
		return company;
	}

	public void setCompany(InspectorCompany company) {
		this.company = company;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.inspectortime.domain.Inspector#getScheduleRules()
	 */
	@OneToMany(targetEntity = ScheduleRuleImpl.class, cascade = { CascadeType.ALL })
	public List<ScheduleRule> getScheduleRules() {
		return scheduleRules;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.inspectortime.domain.Inspector#setScheduleRules(List<com.inspectortime.domain.ScheduleRule>)
	 */
	public void setScheduleRules(List<ScheduleRule> scheduleRules) {
		this.scheduleRules = scheduleRules;

		// Order the scheduleRules according to priority
		Collections.sort(scheduleRules);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.inspectortime.domain.Inspector#getScheduleRuleById(java.io.Serializable)
	 */
	public ScheduleRule getScheduleRuleById(Serializable ruleId) {
		List<ScheduleRule> rules = this.getScheduleRules();
		for (ScheduleRule rule : rules) {
			if (rule.getId().equals(ruleId)) {
				return rule;
			}
		}
		return null;
	}


	/**
	 * Find all inspections associated with this inspector by start and end
	 * dates
	 * 
	 * @param start
	 *            The earliest time for which to return inspections (null means
	 *            start with the earliest found)
	 * @param end
	 *            The latest time for which to return inspections (null means
	 *            include the last found)
	 */
	public List<Inspection> findInspectionsBetween(Date start, Date end) {
		return getServices().getRepository().findInspectionsByReferringAgent(null);
	}

	public int getLimitDrivingPerDayDistance() {
		return limitDrivingPerDayDistance;
	}

	public void setLimitDrivingPerDayDistance(int limitDrivingPerDayDistance) {
		this.limitDrivingPerDayDistance = limitDrivingPerDayDistance;
	}

	public boolean isLimitDrivingPerDay() {
		return limitDrivingPerDay;
	}

	public void setLimitDrivingPerDay(boolean limitDrivingPerDay) {
		this.limitDrivingPerDay = limitDrivingPerDay;
	}


}
